let that;

class Tab{
    constructor(id){
        that = this;
        // 获取元素
        this.main = document.querySelector(id);
        this.add = this.main.querySelector('.tabadd');
        // li的父元素
        this.ul = this.main.querySelector('.first ul:first-child');
        this.section = this.main.querySelector('.tabscon');

        this.init();
    }

    // 获取所有的li和section
    updateNode(){
        // 这些是新增的事物，每次添加都要更新
        this.lis = this.main.querySelectorAll('li');
        this.sections = this.main.querySelectorAll('section');
        // 获取删除按钮
        this.remove = this.main.querySelectorAll('.icon');
        // tab标签
        this.spans = this.main.querySelectorAll('.first li span:first-child');
    }
    init(){
        // init 初始化操作让想让相关的元素绑定
        this.updateNode();
        this.add.onclick = this.addTab;
        for(var i = 0; i < this.lis.length; i++){
            this.lis[i].index = i;
            this.lis[i].onclick = this.toggleTab;
            this.remove[i].onclick = this.removeTab;
            this.spans[i].ondblclick = this.editTab;
            this.sections[i].ondblclick = this.editTab;
        }
    }

    // 1、切换
    toggleTab(){
        that.clearClass();
        this.className = 'liactive';
        that.sections[this.index].className = 'conactive';
    }

    // 清除class
    clearClass(){
        for(var i = 0; i < this.lis.length; i++){
            this.lis[i].className = '';
            this.sections[i].className = '';
        }
    }
    // 2、添加
    // appendChild不支持追加字符串的子元素，通常和createElement配合使用
    // insertAdjacentHTML支持追加字符串的元素
    addTab(){
        that.clearClass();
        let li = '<li class="liactive"><span>新选项卡</span><span class="icon">-</span></li>';
        let section = '<section class="conactive">新主题</section>';
        that.ul.insertAdjacentHTML('beforeend', li);
        that.section.insertAdjacentHTML('beforeend', section);
        that.init();
    }
    // 3、删除
    removeTab(e){
        // 阻止冒泡，防止触发li上的切换点击事件
        e.stopPropagation();
        var index = this.parentNode.index;
        that.lis[index].remove();
        that.sections[index].remove();
        that.init();
        // 我们删除的不是选中状态的li
        if(document.querySelector('.liactive')) return;
        index--;
        // click手动调用，不需要鼠标触发
        that.lis[index] && that.lis[index].click();
    }
    // 4、修改
    editTab(){
        // 双击禁止选中文字
        window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
        let str = this.innerHTML;
        // 生成input标签
        this.innerHTML = '<input type="text" />';
        let input = this.children[0];
        input.value = str;
        input.select();

        // 当我们离开文本框就把文本框里面的值给span
        input.onblur = function(){
            this.parentNode.innerHTML = this.value;
        }

        // 按下回车也能赋值
        input.onkeyup = function(e){
            if(e.keyCode === 13) {
                // 触发blur事件
                this.blur();
            }
        }
    }
}

let tab = new Tab('#main');